package com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.widget.Toast;

/**
 * Created by Administrator on 2018/8/8.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 * <p>
 * 网络检测工具
 */

public class NetworkUtils {
    /**
     * 检测网络连接情况
     */
    public static boolean checkWiFiConnectedState(Context mContext) {
        ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            Boolean isWifiConn = networkInfo.isConnected();
            return isWifiConn;
        } else {
            Boolean networkInfo = checkMConnectedState(connMgr);
            if (networkInfo != null) return networkInfo;
        }
        return false;
    }

    /**
     * @desc api 23 检测网络连接情况
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    private static Boolean checkMConnectedState(ConnectivityManager connMgr) {
        Network[] networks = connMgr.getAllNetworks();
        if (networks != null && networks.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < networks.length; i++) {
                NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                if (networkInfo != null) {
                    return networkInfo.isConnected();
                }
            }
        }
        return null;
    }

    /**
     * @desc 检测移动数据连接情况
     */
    public static boolean checkMobileConnectedState(Context mContext) {
        ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            Boolean isMobileConn = networkInfo.isConnected();
            return isMobileConn;
        } else {
            Boolean networkInfo = checkMConnectedState(connMgr);
            if (networkInfo != null) return networkInfo;
        }
        return false;
    }
    /**
     * @desc 状态判断
     * */
    public static void StateJudge(Context mContext){
            if(checkWiFiConnectedState(mContext) && checkMobileConnectedState(mContext)){
                Toast.makeText(mContext, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show();
            }else if(checkWiFiConnectedState(mContext) && !checkMobileConnectedState(mContext)){
                Toast.makeText(mContext, "WIFI已连接,移动数据已断开", Toast.LENGTH_SHORT).show();
            }else if(!checkWiFiConnectedState(mContext) && checkMobileConnectedState(mContext)){
                Toast.makeText(mContext, "WIFI已断开,移动数据已连接", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(mContext, "WIFI已断开,移动数据已断开", Toast.LENGTH_SHORT).show();
            }
    }
}
